﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Base.Dao;
using T.FAS.Runtime.ScheduleJob.SPI;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.ScheduleJob.Dao
{
    public class ScheduleJobSubscriptionNoticeSetDao : DaoService<ScheduleJobSubscriptionNoticeSet>
    {
        public override string TableName => "FAS_SubscriptionNoticeSet";
        public override string IDColumnName => "ID";

        public override List<string> ExceptColumnName
            => DaoUtil.GetExceptionColumn<ScheduleJobSubscriptionNoticeSet>(
                new List<string>
                {
                }
            );

        private SubscriptionNoticeDefinitionDao definitionDao => new SubscriptionNoticeDefinitionDao();
        public ScheduleJobSubscriptionNoticeSetDao() : base()
        {
            DBName = "TFAS";
        }
        public override ScheduleJobSubscriptionNoticeSet ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            if (columns.ContainsKey("SubscriptionNoticeDefinition"))
                columns.Remove("SubscriptionNoticeDefinition");
            if (columns.ContainsKey("SubscriptionNoticeDefinitionID"))
                columns.Remove("SubscriptionNoticeDefinitionID");
            if (columns.ContainsKey("NoticeContentType"))
                columns.Remove("NoticeContentType");
            var data = base.ConvertToObject(columns,row);



            data.NoticeContentType = (NoticeContentType)Enum.Parse(typeof(NoticeContentType), Convert.ToString(row["NoticeContentType"]));

            var definitionId = Convert.ToString(row["SubscriptionNoticeDefinitionID"]);
            var definition = definitionDao.Get(definitionId);
            if (definition != null)
                data.SubscriptionNoticeDefinition = definition as SubscriptionNoticeDefinition;
            return data;
        }
        public override Dictionary<string, object> GetDataMapping(ScheduleJobSubscriptionNoticeSet data)
        {
            var mapping = base.GetDataMapping(data);
            mapping.Remove("NoticeContentType");
            mapping.Add("NoticeContentType", (int)(data.NoticeContentType));
            return mapping;
        }
        public override Dictionary<string, PropertyInfo> GetColumns()
        {
            return base.GetColumns();
        }

        public void AddScheduleJobSubscriptionNoticeSet(ScheduleJobSubscriptionNoticeSet data)
        {
            base.Add(new List<object>() { data});
        }
        public List<ScheduleJobSubscriptionNoticeSet> GetScheduleJobSubscriptionNoticeSetBySubId(string subId)
        {
            var result = base.GetObjectsByFilter($"ScheduleJobSubscriptionID='{subId}'");
            var list = new List<ScheduleJobSubscriptionNoticeSet>();
            if (result != null && result.Count > 0)
            {
                result.ForEach(x =>
                {
                    var set = x as ScheduleJobSubscriptionNoticeSet;
                    if (set != null && string.IsNullOrEmpty(set.SubscriptionNoticeDefinitionID) && set.SubscriptionNoticeDefinition != null)
                        set.SubscriptionNoticeDefinitionID = set.SubscriptionNoticeDefinition.ID;
                    list.Add(set); 
                }
                );

            }
            return list.OrderBy(x => x.SortOrder).ToList();
        }
        public void DeleteScheduleJobSubscriptionNoticeSet(string subscriptionId)
        {
            var parameter = DB.MakeInParam("@SubscriptionID",TDbDataType.VarChar,subscriptionId);
            DB.ExecSqlStatement(string.Format("delete from {0} where ScheduleJobSubscriptionID = @SubscriptionID",this.TableName),parameter);
        }
    }
}
